<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\CommissionSettle as CommissionSettleModel;

class CommissionSettleService
{
    /**
     * 部门财务
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getCommissionSettle($where, $page, $pagesize)
    {
        $data = CommissionSettleModel::getCommissionSettle($where, $page, $pagesize);
        foreach ($data as $k => $v) {
            // 获取头像
            $headimg = UsersService::findUsersValue(['id' => $v['uid']], 'headimg');
            // 头像/姓名/序号
            $imgHeadimg = '<div><img class="headimgs" title="点我查看原图" onclick="lookimg(this.src)" src="' . $headimg . '" \' + \'alt=""></div>';
            $data[$k]['nickname_data'] = '<div class="d-fix1 mar-lefts">' . $imgHeadimg . '<div><span class="datas">' . $v['username'] . ' / <span class="color3">' . $v['id'] . '</span></span></div></div>';

            // 部门/提成类型
            $data[$k]['branch_data'] = $v['role_name'] . '<br><span class="color3">' . $v['type_name'] . '</span>';

            // 提成点
            $commission_ratio = ($v['commission_ratio'] * 100) . '%';
            // 营业额/提成点
            $data[$k]['trade_data'] = $v['total_turnover'] . '<br><span class="color3">' . $commission_ratio . '</span>';

            // 结算时间
            $sellte_time = $v['sellte_time'] ? date('Y-m-d H:i', $v['sellte_time']) : '-';
            // 佣金/结算时间
            $data[$k]['commission_data'] = $v['total_commission'] . '<br><span class="color3">' . $sellte_time . '</span>';

            // 备注
            $data[$k]['desc'] = $v['desc'] ?: '-';
        }

        return $data;
    }

    /**
     * 统计部门财务
     * @param $where
     * @return mixed
     */
    public static function countCommissionSettle($where)
    {
        return CommissionSettleModel::countCommissionSettle($where);
    }

    /**
     * 单部门财务
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findCommissionSettle($where)
    {
        return CommissionSettleModel::findCommissionSettle($where);
    }

    /**
     * 更新部门财务
     * @param $data
     * @param $where
     * @return CommissionSettleModel
     */
    public static function updateCommissionSettle($data, $where)
    {
        return CommissionSettleModel::updateCommissionSettle($data, $where);
    }

    /**
     * 计算财务
     * @param $where
     * @param string $filed
     * @return float
     */
    public static function sumCommissionSettle($where, $filed = 'total_commission')
    {
        return CommissionSettleModel::sumCommissionSettle($where, $filed);
    }
}